package com.sskj.gocoin.utils;

import android.text.TextUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;

/**
 * Created by lv on 18-6-5.
 */

public class NumberUtil1 {

    public static String keep0(String number) {
        return keepDown(number, 0);
    }

    /**
     * 保留4位小数
     *
     * @param number
     * @return
     */
    public static String keep4(String number) {
        return keepDown(number, 4);
    }

    public static Float keep4f(String number) {
        return Float.parseFloat(keepDown(number, 4));
    }

    public static String keep4(Float number) {
        return keepDown(number, 4);
    }

    public static String keepMax4(String number) {
        return keepMaxDown(number, 4);
    }

    public static String keepMax2(String number) {
        return keepMaxDown(number, 2);
    }

    public static String keepMax2(Double number) {
        return keepMaxDown(number, 2);
    }


    public static String keepHalfMax2(Double number) {
        return keepMaxHalf(number, 2);
    }

    public static String keepHalf2(Double number) {
        return keepHalf(number, 2);
    }


    private static NumberFormat numberFormat;

    /**
     * 保留digit小数位,多余的直接舍去
     *
     * @param number
     * @param digit
     * @return
     */
    public static String keepDown(String number, int digit) {
        if (TextUtils.isEmpty(number)) {
            number = "0";
        }
        if (!isNumeric(number)) {
            return number;
        }

        return format(Double.parseDouble(number), digit, digit, RoundingMode.DOWN);
    }

    /**
     * 保留digit小数位,多余的直接舍去
     *
     * @param number
     * @param digit
     * @return
     */
    public static String keepDown(double number, int digit) {
        return format(number, digit, digit, RoundingMode.DOWN);
    }

    /**
     * 小数位最多为digit,多余舍去
     *
     * @param number
     * @param digit
     * @return
     */
    public static String keepMaxDown(String number, int digit) {
        if (TextUtils.isEmpty(number)) {
            number = "0";
        }
        if (!isNumeric(number)) {
            return number;
        }
        return format(Double.parseDouble(number), 0, digit, RoundingMode.DOWN);
    }

    /**
     * 小数位最多为digit,多余舍去
     *
     * @param number
     * @param digit
     * @return
     */
    public static String keepMaxDown(Double number, int digit) {
        return format(number, 0, digit, RoundingMode.DOWN);
    }

    /**
     * 小数位最多为digit,多余舍去
     *
     * @param number
     * @param digit
     * @return
     */
    public static String keepMaxHalf(Double number, int digit) {
        return format(number, 0, digit, RoundingMode.HALF_UP);
    }


    public static String keepHalf(Double number, int digit) {
        return format(number, digit, digit, RoundingMode.HALF_UP);
    }


    private static String format(Object number, int minDigist, int maxDigit, RoundingMode roundingMode) {
        if (numberFormat == null) {
            numberFormat = NumberFormat.getNumberInstance();
            //不以逗号分割
            numberFormat.setGroupingUsed(false);
        }
        numberFormat.setRoundingMode(roundingMode);
        numberFormat.setMinimumFractionDigits(minDigist);
        numberFormat.setMaximumFractionDigits(maxDigit);
        return numberFormat.format(number);
    }
    public static double formatDouble(double number,int digist){
        BigDecimal bigDecimal=new BigDecimal(number);
        return  bigDecimal.setScale(digist,BigDecimal.ROUND_HALF_UP).doubleValue();
    }



    public static boolean isNumeric(String str) {
        String reg = "^[0-9]+(.[0-9]+)?$";
        return str.matches(reg);
    }
}
